package com.openstackapi.ctds.imp;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.api.compute.ComputeFloatingIPService;
import org.openstack4j.api.compute.ComputeService;
import org.openstack4j.api.compute.ServerService;
import org.openstack4j.api.networking.NetFloatingIPService;
import org.openstack4j.api.networking.NetworkService;
import org.openstack4j.api.networking.NetworkingService;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.Address;
import org.openstack4j.model.compute.Addresses;
import org.openstack4j.model.compute.FloatingIP;
import org.openstack4j.model.compute.Server;
import org.openstack4j.model.identity.v2.Tenant;
import org.openstack4j.model.network.NetFloatingIP;
import org.openstack4j.model.network.Network;
import org.openstack4j.openstack.OSFactory;
import org.openstack4j.openstack.networking.domain.NeutronFloatingIP;
import org.openstack4j.openstack.networking.internal.FloatingIPServiceImpl;

import com.openstackapi.ctds.IPController;
import com.openstackapi.ctds.pojo.ApplyIPResp;
import com.openstackapi.ctds.pojo.BindingIPResp;
import com.openstackapi.ctds.pojo.ReleaseIPResp;
import com.openstackapi.ctds.pojo.UnbindIPResp;

public class IPControllerImp implements IPController {
	 
	private FloatingIPServiceImpl floatingIPServiceImpl;

	@Override
	public ApplyIPResp ApplyIP(String ipType, String appID, String appName) {
		//登录认证
		OSClientV2 os = OSFactory.builderV2()
		                .endpoint("http://10.50.0.11:5000/v2.0")
		                .credentials("admin","ADMIN_PASS")
		                .tenantName("admin")
		                .authenticate();
		

		ComputeFloatingIPService a = os.compute().floatingIps();
		
	    
	    
//	    NetFloatingIP  floating = netWorkFloatingService.create();
		
		
		
		return null;
	}

	@Override
	public ReleaseIPResp ReleaseIP(String ip) {
		
		//登录认证
		OSClientV2 os = OSFactory.builderV2()
		                .endpoint("http://10.50.0.11:5000/v2.0")
		                .credentials("admin","ADMIN_PASS")
		                .tenantName("admin")
		                .authenticate();
		

		ComputeFloatingIPService cfis = os.compute().floatingIps();
	
		ActionResponse ar = cfis.deallocateIP(ip); 
		
		return null;
	}

	@Override
	public BindingIPResp BindingIP(String ip, String resourceID) {
	
		
		//登录认证
		OSClientV2 os = OSFactory.builderV2()
		                .endpoint("http://10.50.0.11:5000/v2.0")
		                .credentials("admin","ADMIN_PASS")
		                .tenantName("admin")
		                .authenticate();
		

		ComputeService computeService= os.compute();
		ComputeFloatingIPService computeFloatingIPService = computeService.floatingIps();
		
		List<String> str = computeFloatingIPService.getPoolNames();
		
		
		
//		for(String name:str){
//			System.out.println(name);
//		}
		/**
		 * -----pool begin-----
			基础基础
			FloatIP
			基础网络
			-----pool end-----
		 */
		
		FloatingIP floationgIP = computeFloatingIPService.allocateIP("FloatIP");
		
		
		//list server
		Server server = os.compute().servers().get("f8d4a12e-2380-4a6b-85f1-01971a2f010e");
		
		//add floatingIP to server
		ActionResponse ar = computeFloatingIPService.addFloatingIP(server, floationgIP.getFloatingIpAddress());
		System.out.println(ar);
				
		return null;
	}

	@Override
	public UnbindIPResp UnbindIP(String ip, String resourceID) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
	
	

	//TEST 测试
	public static void main(String[] args){
	

			
			//登录认证
			OSClientV2 os = OSFactory.builderV2()
			                .endpoint("http://10.50.0.11:5000/v2.0")
			                .credentials("admin","ADMIN_PASS")
			                .tenantName("admin")
			                .authenticate();
			
			ComputeService  computeService = os.compute();
			ComputeFloatingIPService  computeFloatingIPService = computeService.floatingIps();
			
			ServerService serverService = os.compute().servers();
			Server server = serverService.get("0de6753b-0d41-49da-957e-e449438b56ec");
			System.out.println(server.getAddresses());
			Addresses addresses = server.getAddresses();
			Map<String, List<? extends Address>>  map = addresses.getAddresses();
			System.out.println(map);
			
	}
	
	

	
	
}
